<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reply extends CI_Controller {

	function __construct(){
		parent::__construct();
		authRedirect('/admin/index');
		$this->load->model('production_model');
		$this->load->model('zixun_model');
	}
	public function index()
	{
		$data['error'] = $this->session->flashdata('error');
		$data['success'] = $this->session->flashdata('success');
		

		$id = (int)$this->uri->segment(4);
		if(!$id) show_error('错误的操作');

		$p = $this->production_model->get_production_byid($id);
		if(!$p) show_error('该商品可能已经被删除');
		$data['p'] = $this->production_model->get_production_byid($id);
		
		$start = $this->uri->segment(6,0);
		$point_type = (int)$this->uri->segment(5,1);
		$data['c'] = $this->zixun_model->get_zixun($id,$point_type,$start,PAGENUM);
		$data['num'] = $this->zixun_model->get_zixun_num($id,$point_type);

		//pagenation
		$this->load->library('pagination');
		$config['base_url'] = '/zixun/action/' . $id . '/' . $point_type . '/';
		$config['total_rows'] = $data['num'];
		$config['per_page'] = PAGENUM; 
		$config['full_tag_open'] = '<p class="page">';
		$config['full_tag_close'] = '</p>';
		$config['uri_segment'] = 6;
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['cur_tag_open'] = '<b>';
		$config['cur_tag_close'] = '</b>';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		
		$data['point_type'] = $point_type;


		$this->load->view('admin/reply/list.tpl',$data);
	}

	public function action(){
		
		//咨询条目ID
		$cid = (int)$this->uri->segment(4,0);
		if(!$cid){
			echo json_encode(array('status'=>false,'msg'=>'这个问题不存在!'));exit;
		}
		$content = trim($this->input->post('content'));
		if(!$content){
			echo json_encode(array('status'=>false,'msg'=>'添加回复内容吧!'));exit;
		}

		$r = $this->zixun_model->add_reply($cid,$content);
		if($r['status']){
			echo json_encode(array('status'=>true,'msg'=>'添加回复内容吧!'));exit;
		}else{
			echo json_encode(array('status'=>false,'msg'=>'回复失败，可能网络问题!'));exit;
		}
		
	}

	public function update(){
		$id = (int)$this->uri->segment(4);
		if(!$id) show_error('错误的操作');

		$p = $this->production_model->get_production_byid($id);
		if(!$p) show_error('该商品可能已经被删除');

		$this->load->library('form_validation');
		if($this->form_validation->run('add_production')){
			$r = $this->production_model->update($id);
			if($r['status']){
				$this->session->set_flashdata('success','更新成功！');
				redirect('/admin/production');
			}else{
				$this->session->set_flashdata('error',$r['msg']);
				redirect('/admin/production/action/' . $id);
			}
		}else{
			$this->session->set_flashdata('error',validation_errors());
			redirect('/admin/production/action/' . $id);
		}
	}
}

/* End of file production.php */
/* Location: ./application/controllers/admin/production.php */